package Network;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import BasicClient.Constants;

public class IncomingPlayer extends Thread{

	private Socket sock;
	private NetworkHoldemDealer dealer;
	
	public IncomingPlayer(Socket sock, NetworkHoldemDealer dealer){
		this.sock = sock;
		this.dealer = dealer;
	}
	
	/**
	 * Checks that the incoming request is a request for a Player ID 
	 * @return
	 */
	private boolean isClientRequestingAnID(String request){
		boolean returnValue = false;
		
		return returnValue;
	}
	
	public void run(){
		System.out.println("Waiting for player id request");
		
		
		DataInputStream in = null;
		String incomingRequest;
		int playerID;
		try {
			//block until the proper request comes in
			in = new DataInputStream(sock.getInputStream());
			while(in.readByte() != Constants.PlayerBroadcastConstants.REQUEST_ID_HEADER){
				System.out.println("Accepted player id request!");
				playerID = in.readInt();
				break;
				// playerID read in here should be 0xFFFFFFFF, not sure why we have this 
				// special player id. Continue as normal.
			}
			
			playerID = dealer.getNewPlayerID();
			
			//create RPP and give to PD
			RemotePokerPlayer rpp = new RemotePokerPlayer(sock, playerID, dealer);
			dealer.addObservingPlayer(rpp);
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
